home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 476-500 / disk_477 / irmaster / irc.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  27KB  |  1,055 lines

  1. /*------------------------------- i r c . c  -------------------------------*/
  2. /* Experimental interrupt handler.                                          */
  3. /* This routine sets up a level 2 interrupt server that is triggered by the */
  4. /* ACK pin of the parallel port going low.  The interrupt server then uses  */
  5. /* Timer B in CIA-B to time reading in data from the parallel port.         */
  6. /* The ACK and one data input pin on the parallel port are tied             */
  7. /* together by the hardware I built.  Initially the ACK interrupt is        */
  8. /* enabled and the software sits and waits for something to happen.         */
  9. /*                                                                          */
  10. /* NEED TO CHECK RESOURCES  ---- FREEMEM!!!!!!!!!!!!!!!!*/
  11. /*--------------------------------------------------------------------------*/
  12.  
  13. /*--- Includes and Defines. */
  14.  
  15. #include "irc.h"
  16. #include "IRMaster.h"   /* Definitions of Screen, Window and gadgets. */
  17.  
  18. /* DBG = 1 --> Turn on debug printf's, DBG = 0 --> Turn off debug printf's. */
  19. #define DBG 1
  20. /* Set DEBUG to 0 to comment out code related to opening screen and window.*/
  21. #define DEBUG 1
  22.  
  23. #define NOERROR 0
  24. #define ERROR 1
  25. /*--- NSAMPLES is the number of data samples to be read into buffer. */
  26. #define NSAMPLES 3000
  27. /*--- TOLERENCE is the number of samples difference that is tolerated in */
  28. /*--- order to match two patterns. */
  29. #define TOLERENCE 50
  30.  
  31. /*--- External system variables. */
  32.  
  33. extern struct Custom custom;
  34. extern struct CIA ciab;
  35.  
  36. extern struct GfxBase *GfxBase;
  37. extern struct IntuitionBase *IntuitionBase;
  38. extern struct WBStartup *WBenchMsg;  /* For Workbench startup. */
  39.  
  40. /**********************************************************/  
  41. /***************** Timer B Interrupt code *****************/
  42. /**********************************************************/  
  43. /*
  44.    Update Timer every TIME_SLICE microseconds.
  45.    TIME_SLICE = 1.397 * (desired # of microseconds) [71 = 100us]
  46.    71, 50 work.
  47. */
  48. #define TIME_SLICE ((unsigned short)35)
  49.  
  50. /* Defines to make the code more readable. */
  51.  
  52. #define ciatlo        ciab.ciatblo
  53. #define ciathi        ciab.ciatbhi
  54. #define ciacr        ciab.ciacrb
  55. #define ciaicr        ciab.ciaicr
  56.  
  57. int TimerSigBit = -1;   /* Allocated signal bit. */
  58. ULONG TimerSigMask;      /* TimerSigBit converted into a mask. */
  59. static struct Library *CIAResource = NULL;
  60. struct Task *thisTask;
  61.  
  62. int NSamples;           /* Number of samples in buffer. */
  63.  
  64. int SetUpTimer()
  65. {
  66.    char temp;
  67.    unsigned short micros;
  68.  
  69.    /*--- Check to see if timer B in CIA-B is already in use. */
  70.    /*--- If START bit is set timer is probably being used. */
  71.  
  72. #if DBG
  73.    printf("\n Timer B control register = %X",ciab.ciacrb);
  74. #endif
  75.    if(ciab.ciacrb & 0x01)
  76.    {
  77.       printf("\n Timer B is already allocated.  Proceding anyway.");
  78.    }
  79.  
  80.    /*--- Set latched value for timer to count down from. */
  81.  
  82.    micros = TIME_SLICE;
  83. #if DBG
  84.    printf("\n Sampling interval = %d * 1.397 microseconds", micros);
  85. #endif
  86.    ciatlo = micros & 0xFF;
  87.    ciathi = micros >> 8;
  88.  
  89.    /*--- Get ID for this task so can send it a signal. */
  90.  
  91.    thisTask = NULL;
  92.    thisTask = FindTask(NULL);
  93.    if(thisTask == NULL)
  94.    {
  95.       printf("\n Error - Can't find this task ID.");
  96.       return(ERROR);
  97.    }
  98.  
  99.    /*--- Get a signal bit. */
  100.  
  101.    if((TimerSigBit = AllocSignal(-1L)) == -1)
  102.    {
  103.       printf("\n Timer: AllocSignal failed.");
  104.       StopTime();  /* Deallocate resources. */
  105.       return(ERROR);
  106.    }
  107.    TimerSigMask = 1L << TimerSigBit;
  108.  
  109.    /*--- Open the CIA resource. */
  110.  
  111.    if((CIAResource = OpenResource(CIABNAME)) == NULL)
  112.    {
  113.       printf("\n Timer: Couldn't open %s.", CIABNAME);
  114.       StopTime();  /* Deallocate resources. */
  115.       return(ERROR);
  116.    }
  117.  
  118.    /*--- Interrupts have been enabled so may already */
  119.    /*--- have an interrupt.  So do this... */
  120.  
  121.    ciab.ciacrb &= ~CIACRBF_START;    /* stop timer */
  122.  
  123.    temp = ciaicr;  /* Read IRC register to clear interrupt. */
  124.  
  125.    SetSignal(0, TimerSigMask);    /* clear signal */
  126.  
  127. #if DBG
  128.    printf("\n About to set RUNMODE");
  129. #endif
  130.    ciacr &= ~CIACRBF_RUNMODE; /* Set it to reload upon underflow. */
  131.    ciacr &= ~CIACRBF_PBON; /* Output line PB7 is left alone. */
  132.    ciacr &= ~CIACRBF_INMODE0; /* Count clock pulses. */
  133.    ciacr &= ~CIACRBF_INMODE1; /* Count clock pulses. */
  134.  
  135. #if DBG
  136.    printf("\n About to strobe LOAD");
  137. #endif
  138.    ciacr |= CIACRBF_LOAD; /* Strobe Load to latch in countdown value. */
  139.  
  140. #if DBG
  141.    printf("\n About to enable timer b interrupts");
  142. #endif
  143.    ciaicr = 0x7D;  /* Disable all other CIA-B interrupts. */
  144.    ciaicr = CIAICRF_SETCLR|CIAICRF_TB;  /* Enable timer B interrupts. */
  145. #if DBG
  146.    printf("\n Enabled timer b interrupts");
  147. #endif
  148.  
  149.    return(NOERROR);
  150. }
  151.  
  152. int StopTime()
  153. {
  154.    if(thisTask != NULL)
  155.    {
  156.       /*--- Disable timer B interrupts. */
  157.  
  158. #if DBG
  159.       printf("\n Stopping timer B.");
  160. #endif
  161.       ciaicr = CIAICRF_TB;
  162.  
  163.       /*--- Halt timer. */
  164.  
  165.       ciacr &= ~CIACRBF_START;
  166.  
  167.       /*--- Free resources. */
  168.  
  169.       if(TimerSigBit != -1)
  170.       {
  171. #if DBG
  172.          printf("\n Freeing signal bit.");
  173. #endif
  174.          FreeSignal(TimerSigBit);
  175.       }
  176.    }
  177.  
  178.    return(NOERROR);
  179. }
  180.  
  181. /**********************************************************/  
  182. /***************** LEVEL 2 Interrupt code *****************/
  183. /**********************************************************/  
  184.  
  185. extern void ircserver();
  186. struct Interrupt intrpt;
  187.  
  188. int AddINT2Server()
  189. {
  190.    /*--- init NODE structure */
  191.  
  192.    intrpt.is_Node.ln_Type = NT_INTERRUPT;
  193.    /* Was 0 but glasses flickered.  3 fixed it. */
  194.    intrpt.is_Node.ln_Pri  = 127;
  195.    intrpt.is_Node.ln_Name = "WWB";
  196.    intrpt.is_Code        = ircserver;
  197.    intrpt.is_Data        = NULL;
  198.  
  199. #if DBG
  200.    printf("\n About to add interrupt server...");
  201. #endif
  202.  
  203.    AddIntServer(INTB_PORTS, &intrpt);        /* Add server to chain */
  204.  
  205. #if DBG
  206.    printf("\n Added interrupt server.");
  207. #endif
  208.  
  209.    return(NOERROR);
  210. }
  211.  
  212.  
  213. int RemoveServer()
  214. {
  215.    RemIntServer(INTB_PORTS, &intrpt);
  216.  
  217.    return(NOERROR);
  218. }
  219.  
  220.  
  221. /**********************************************************/  
  222. /************************** MAIN **************************/
  223. /**********************************************************/  
  224.  
  225. unsigned char *buffer;  /* Pointer to storage for data to be read in. */
  226. unsigned char zuffer[NSAMPLES];  /* Storage for data to be read in. */
  227. unsigned char pattern[20][NSAMPLES];  /* Stored signal patterns. */
  228. char *address, mask;
  229.  
  230. struct Screen *Screen = NULL;
  231. struct Window *Window = NULL;
  232. struct RastPort *WRPort;
  233. struct ViewPort *WVPort;
  234.  
  235. struct IntuiMessage *message;
  236. ULONG class;
  237. USHORT code;
  238. struct Gadget *igad;
  239. USHORT gadget_id;
  240. extern int BackGround();
  241. int helptxt();
  242. int IRButtonOff();                 /* Unhighlight button. */
  243. int IRButtonOn();                  /* Highlight button. */
  244. int IRButtonLabel();               /* Relabel button. */
  245. int GetPattern();                  /* Get signal pattern. */
  246. int DisplayPattern();              /* Draw the signal pattern. */
  247. int DPattern();                    /* Debug. */
  248.  
  249. /*--- Buffers for area fill and text. */
  250.  
  251. WORD chip areaArray[100];  /* Max of 20 vertices times 5 words per vertex. */
  252. struct AreaInfo myAreaInfo;
  253. PLANEPTR workspace;
  254. struct TmpRas myTmpRas;
  255.  
  256. #define NOTHING 0
  257. int active[21] =  /* Used to remember which buttons have patterns. */
  258.    FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
  259.    FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
  260.    FALSE
  261. };
  262.  
  263.  
  264.  
  265.  
  266. main(argc, argv)
  267. int argc;
  268. char **argv;
  269. {
  270.    int it, error, iz, is, try;
  271.    int diff[4], match, smallest;
  272.    BOOL exitflag;
  273.    int mode;  /* Current mode of program (LEARN, NOTHING, etc. ) */
  274.    int IRButtonSelected = -1;         /* Index of button currently selected. */
  275.  
  276.    /*--- Ignore CTRL-C and CTRL-D. */
  277.  
  278.    signal(SIGINT, SIG_IGN);
  279.  
  280.    /*--- Open graphics and Intuition Libraries. */
  281.  
  282.    GfxBase = NULL;
  283.    GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", 33);
  284.    if(GfxBase == NULL)
  285.    {
  286.       printf("\n Error opening graphics library.");
  287.       goto ShutDown;
  288.    }
  289.  
  290.    IntuitionBase = NULL;
  291.    IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library", 33);
  292.    if(IntuitionBase == NULL)
  293.    {
  294.       printf("\n Error opening Intuition library.");
  295.       goto ShutDown;
  296.    }
  297.  
  298.    /*--- Open a new screen. */
  299.  
  300.    Screen = (struct Screen *) OpenScreen(&NewScreenStructure); 
  301.    if(Screen == 0L)
  302.    {
  303.       printf("\n Error opening a new screen.");
  304.       goto ShutDown;
  305.    }
  306.  
  307. #if DEBUG
  308.    /*--- Open window in screen. */
  309.  
  310.    NewWindowStructure1.Screen = Screen;
  311.  
  312.    Window = (struct Window *) OpenWindow(&NewWindowStructure1);
  313.  
  314.    if(Window == 0L)
  315.    {
  316.       printf("\nError opening a new window");
  317.       goto ShutDown;
  318.    }
  319.  
  320.    /*--- Get pointers to window structures. */
  321.  
  322.    WRPort = Window->RPort;   /* Get pointer to windows' RastPort. */
  323.    WVPort = (struct ViewPort *)ViewPortAddress(Window); /* Get pointer to window viewport. */
  324.  
  325.    /*--- Set up area fill and text work buffers. */
  326.  
  327.    InitArea(&myAreaInfo, &areaArray[0], 20);
  328.    WRPort->AreaInfo = &myAreaInfo;
  329.  
  330.    workspace = AllocRaster(640, 200);
  331.    if(workspace == 0)
  332.    {
  333.       printf("\n Error - No space for temporary raster.");
  334.       goto ShutDown;
  335.    } else
  336.    {
  337.       InitTmpRas(&myTmpRas, workspace, RASSIZE(640,200));
  338.       WRPort->TmpRas = &myTmpRas;
  339.    }
  340.  
  341.    /*--- Set color map. */
  342.  
  343.    LoadRGB4(WVPort, Palette, (SHORT)16);
  344.  
  345.    /*--- Draw raised background areas. */
  346.  
  347.    BackGround();
  348.  
  349.    /*--- Redraw the gadgets on top of the raised background. */
  350.  
  351.    RefreshGadgets(&ARexx, Window, 0);
  352. #endif /*DEBUG*/
  353.  
  354.    /*--- Allocate the parallel port resource so that other tasks know it  */
  355.    /*--- is taken. (This might not be a good idea.  Online! allocates the */
  356.    /*--- the parallel port and this prevents me from doing                */
  357.    /*--- "copy file to par:" in a seperate window.  So leave it out for   */
  358.    /*--- now.  User will know if one program walks over another.          */
  359.  
  360.    /*****************
  361.    MR_ALLOCMISCRESOURCE(MR_PARALLELPORT, "IRMaster");
  362.    MR_ALLOCMISCRESOURCE(MR_PARALLELBITS, "IRMaster");
  363.    *****************/
  364.  
  365.    /*--- Set up pointer to data buffer for SoftHandler(). */
  366.    /*--- Assembler doesn't seem to recognize zuffer but DOES buffer. */
  367.  
  368.    buffer = &zuffer[0];
  369. #if DBG
  370.    printf("\n buffer = %X", buffer);
  371. #endif
  372.  
  373.    /*--- Set up all the interrupt servers. */
  374.  
  375.    AddINT2Server();
  376.  
  377.    error = SetUpTimer();
  378.    if(error)goto Fini;
  379.  
  380.    /*--- Explicitly enable level6 and level2 interrupts (even though */
  381.    /*--- they already seem to be enabled by default).                */
  382.  
  383. /********* Causes sometimes guru.
  384.    Disable();
  385.    custom.intena |= INTF_SETCLR | INTF_EXTER;
  386.    custom.intena |= INTF_SETCLR | INTF_SOFTINT;
  387.    Enable();
  388. *********/
  389.  
  390. #if DBG
  391.    /* This is slightly dangerous since it might clear any pending interrupts. */
  392.    /*printf("\n Enabled interrupts are: %X", custom.intenar);*/
  393. #endif
  394.  
  395.    /*--- Set address for IRC register (used in GetSample.) */
  396.  
  397.    address = (char *)0xBFED01;/* Address of the Interrupt Control Register in 8520-A */
  398.  
  399.    /*--- MAIN PROGRAM LOOP. */
  400.    /*--- Get messages from window until user presses CloseWindow gadget. */
  401.  
  402. #if DEBUG
  403.    exitflag = FALSE;
  404.    mode = NOTHING;
  405.    while(exitflag == FALSE)
  406.    {
  407.       /*--- If in DOIT mode wait for IR input. */
  408.  
  409.       if(mode == DOIT)
  410.       {
  411.          /*--- Wait for user to press a button. */
  412.  
  413.          GetSample();
  414.  
  415.          /*--- Match input to stored samples. */
  416.  
  417.          match = find_match();
  418.       }
  419.  
  420.       /*--- Check for user action. */
  421.  
  422.       message = (struct IntuiMessage *)GetMsg(Window->UserPort);
  423.       if(message != 0)
  424.       {
  425.          class = message->Class;
  426.          code = message->Code;
  427.          igad = (struct Gadget *)message->IAddress;/*get pointer to gadget */
  428.          ReplyMsg((struct Message *)message);
  429.  
  430.          if(class == GADGETUP)
  431.          {
  432.             gadget_id = igad->GadgetID;   /* get my gadget code */
  433.             switch(gadget_id)
  434.             {
  435.                case COMMANDSTR3:
  436. #if DBG
  437.                   printf("COMMANDSTR3\n");
  438. #endif
  439.                   break;
  440.  
  441.                case COMMANDSTR1:
  442. #if DBG
  443.                   printf("COMMANDSTR1 = %s\n", commandstr1SIBuff);
  444. #endif
  445.                   /*--- Change the label on the button. */
  446.  
  447.                   IRButtonLabel(commandstr1SIBuff, IRButtonSelected);
  448.  
  449.                   /*--- Prompt user for next action. */
  450.  
  451.                   helptxt("Now press the button on your handheld IR remote control",
  452.                      "that you want memorized.");
  453.  
  454.                   /*--- Get verified signal sample from the IR receiver. */
  455.  
  456.                   GetPattern(IRButtonSelected);
  457.  
  458.                   /*--- Remember which buttons have patterns stored. */
  459.  
  460.                   active[IRButtonSelected] = TRUE;
  461.  
  462.                   /*--- Unhighlight the control button. */
  463.  
  464.                   IRButtonOff(&IRButtonSelected);
  465.                   mode = NOTHING;
  466.  
  467.                   break;
  468.  
  469.                case COMMANDSTR2:
  470. #if DBG
  471.                   printf("COMMANDSTR2\n");
  472. #endif
  473.                   break;
  474.  
  475.               default:
  476.                   break;
  477.             } /* switch(gadget_id) */
  478.          } /* if(GADGETUP) */
  479.  
  480.          if(class == GADGETDOWN)
  481.          {
  482.             gadget_id = igad->GadgetID;   /* get my gadget code */
  483.             switch(gadget_id)
  484.             {
  485.                case EXIT:
  486. #if DBG
  487.                   printf("EXIT\n");
  488. #endif
  489.                   goto Fini;
  490.                   break;
  491.  
  492.                case AUTHOR:
  493. #if DBG
  494.                   printf("AUTHOR\n");
  495. #endif
  496.                   break;
  497.  
  498.                case DOIT:
  499. #if DBG
  500.                   printf("DOIT\n");
  501. #endif
  502.                   mode = DOIT;
  503.                   break;
  504.  
  505.                case GETSET:
  506. #if DBG
  507.                   printf("GETSET\n");
  508. #endif
  509.                   break;
  510.  
  511.                case SAVESET:
  512. #if DBG
  513.                   printf("SAVESET\n");
  514. #endif
  515.                   break;
  516.  
  517.                case LEARN:
  518. #if DBG
  519.                   printf("LEARN\n");
  520. #endif
  521.                   helptxt("Press one of the twenty control buttons above.","");
  522.                   mode = LEARN;
  523.                   break;
  524.  
  525.                case AREXX:
  526. #if DBG
  527.                   printf("AREXX\n");
  528. #endif
  529.                   break;
  530.  
  531.                case IR20:
  532.                case IR19:
  533.                case IR18:
  534.                case IR17:
  535.                case IR16:
  536.                case IR15:
  537.                case IR14:
  538.                case IR13:
  539.                case IR12:
  540.                case IR11:
  541.                case IR10:
  542.                case IR9:
  543.                case IR8:
  544.                case IR7:
  545.                case IR6:
  546.                case IR5:
  547.                case IR4:
  548.                case IR3:
  549.                case IR2:
  550.                case IR1:
  551.                   /*--- Unhighlight any currently selected button. */
  552.  
  553.                   IRButtonOff(&IRButtonSelected);
  554.  
  555.                   /*--- Set current button to highlight and highlight it. */
  556.  
  557.                   IRButtonSelected = (gadget_id - IR1) + 1;
  558.  
  559.                   IRButtonOn(IRButtonSelected);
  560. #if DBG
  561.                   printf("IR-%d\n", (gadget_id - IR1) + 1);
  562. #endif
  563.                   if(mode == LEARN)
  564.                   {
  565.                      /*--- Prompt user. */
  566.  
  567.                      helptxt("Enter the label name for the control button,",
  568.                         "then press RETURN.");
  569.                   } else
  570.                   {
  571.                      helptxt("Please choose a function button first.", "");
  572.                   }
  573.                   break;
  574.  
  575.  
  576.                default:
  577.                   mode = NOTHING;
  578.                   printf("Error - Got a hit on an unknown gadget\n");
  579.                   printf(" Gadget id was: hex: %X  decimal: %d\n", gadget_id,
  580.                      gadget_id);
  581.                   break;
  582.             } /* switch(gadget_id) */
  583.          } /* if(message) */
  584.       } /* if(class...) */
  585.    } /* while(GetMsg) */
  586. #endif /*DEBUG*/
  587.  
  588.  
  589. #if DBG
  590.    printf("\n Timer B control register = %X",ciab.ciacrb);
  591.    printf("\n buffer = %X", buffer);
  592. #endif
  593.  
  594.    /*--- Shut down timer B */
  595.  
  596. Fini:
  597.    StopTime();
  598.  
  599.    /*--- Turn off the 8520's recognition of the ACK as an interrupt. */
  600.  
  601.    mask = 0x10; /* Hex 10 = 00010000 Should disable the 8520 FLAG interrupt. */
  602.    *address = mask;  /* Disable the interrupt. */
  603.  
  604.    /*--- Remove the level2 interrupt server from server chain. */
  605.  
  606.    RemoveServer();
  607.  
  608.    /*--- Exit cleanly. */
  609.  
  610. ShutDown:
  611.  
  612.    /*--- Deallocate parallel port resource. Commented out for reasons above. */
  613.  
  614.    /*****************
  615.    MR_FREEMISCRESOURCE(MR_PARALLELPORT);
  616.    MR_FREEMISCRESOURCE(MR_PARALLELBITS);
  617.    *****************/
  618.  
  619.    /*--- Close window and screen. */
  620.  
  621. #if DEBUG
  622.    if(Window)CloseWindow(Window);
  623.    if(Screen)CloseScreen(Screen);
  624. #endif /*DEBUG*/
  625.  
  626.    /*--- Close libraries. */
  627.  
  628.    if(GfxBase)CloseLibrary((struct Library *)GfxBase);
  629.    if(IntuitionBase)CloseLibrary((struct Library *)IntuitionBase);
  630.  
  631.    /*--- Free allocated memory. */
  632. #if DEBUG
  633.    if(workspace != 0)FreeRaster(workspace, 640, 200);
  634. #endif /*DEBUG*/
  635.  
  636.    return(0);  /* To keep compiler happy. */
  637. } /* End of main(). */
  638.  
  639.  
  640.  
  641. int GetSample()
  642. {
  643.    int idx;
  644.  
  645.    /*--- Set up for taking another sample. */
  646.  
  647. #if DBG
  648.    printf("\n Setting up for next sample.");
  649. #endif
  650.    for(idx=0; idx<NSAMPLES; idx++)zuffer[idx] = 0; /* Zero the buffer. */
  651.    NSamples = NSAMPLES;  /* Reset sample counter. */
  652.    buffer = &zuffer[0];  /* Reset pointer to start of buffer. */
  653.  
  654.    ciacr |= CIACRBF_LOAD; /* Strobe Load to latch in countdown value. */
  655.    ciaicr = CIAICRF_SETCLR|CIAICRF_TB;  /* Enable timer B interrupts. */
  656.    mask = *address;  /* Clear any pending interrupts. */
  657.    mask = 0x90;  /* Hex 90 = 10010000 Should enable the 8520 FLAG interrupt. */
  658.    *address = mask;  /* Enable the ACK interrupt. */
  659.  
  660.    /*--- Interrupts are now all armed and dangerous.  */
  661.    /*--- Wait until data is ready. */
  662.  
  663. #if DBG
  664.    printf("\n Waiting...press a button.");
  665. #endif
  666.    Wait(TimerSigMask);  /* Wait for full buffer. */
  667.  
  668.    /*--- Stop timer B from counting. */
  669.  
  670. #if DBG
  671.    printf("\n Stopping timer B");
  672. #endif
  673.    ciacr &= ~CIACRBF_START;
  674.  
  675.    /*--- Wait until user lets go of button. */
  676.  
  677.    LetGo();
  678.  
  679.    return(0);
  680. }
  681.  
  682. #if DEBUG
  683. /*-------------------- h e l p t x t . c ------------------------*/
  684. /* This routine clears the help message area and writes a new    */
  685. /* message into it.                                              */
  686. /*---------------------------------------------------------------*/
  687.  
  688. int helptxt(msg1, msg2)
  689.   char *msg1, *msg2;
  690. {
  691.  
  692.    /*--- Erase any old text that exists. */
  693.  
  694.    SetAPen(WRPort, 0);
  695.    RectFill(WRPort, 24, 163, 617, 188);
  696.  
  697.    /*--- Write new text. */
  698.  
  699.  
  700.    SetAPen(WRPort, 1);
  701.    Move(WRPort, 25, 173);
  702.    Text(WRPort, msg1, strlen(msg1));
  703.    Move(WRPort, 25, 185);
  704.    Text(WRPort, msg2, strlen(msg2));
  705.  
  706.    return(0);
  707. }
  708.  
  709. /*---------------------- I R B u t t o n s O f f . c ---------------------*/
  710. /* This routine unhighlights any currently selected control button.       */
  711. /*------------------------------------------------------------------------*/
  712.  
  713. int IRButtonOff(button)
  714.   int *button;  /* Button to turn off. */
  715. {
  716.    USHORT position;
  717.  
  718.    if((*button >= 1) && (*button <= 20))
  719.    {
  720.       /*--- Remove currently selected gadget from list. */
  721.  
  722.       position = RemoveGList(Window, IR[*button-1], 1L);
  723.  
  724.       /*--- Unselect the gadget. */
  725.  
  726.       IR[*button-1]->Flags &= (~SELECTED);
  727.  
  728.       /*--- Put the gadget back into the same place in the list. */
  729.  
  730.       AddGList(Window, IR[*button-1], (LONG)position, 1L, NULL);
  731.  
  732.       /*--- Refresh gadgets on display. */
  733.  
  734.       RefreshGList(IR[*button-1], Window, NULL, 1L);
  735.  
  736.       /*--- Set currently selected gadget to none. */
  737.  
  738.       *button = -1;
  739.    }
  740.  
  741.    return(TRUE);
  742. }
  743.  
  744.  
  745. /*---------------------- I R B u t t o n s O n . c -----------------------*/
  746. /* This routine highlights the currently selected control button.         */
  747. /*------------------------------------------------------------------------*/
  748.  
  749.  
  750. int IRButtonOn(button)
  751.   int button;  /* Button number to highlight. */
  752. {
  753.    USHORT position;
  754.  
  755.    if((button >= 1) && (button <= 20))
  756.    {
  757.       /*--- Remove the selected gadget from list. */
  758.  
  759.       position = RemoveGList(Window, IR[button-1], 1L);
  760.  
  761.       /*--- Select the gadget. */
  762.  
  763.       IR[button-1]->Flags |= SELECTED;
  764.  
  765.       /*--- Put the gadget back into the same place in the list. */
  766.  
  767.       AddGList(Window, IR[button-1], (LONG)position, 1L, NULL);
  768.  
  769.       /*--- Refresh gadgets on display. */
  770.  
  771.       RefreshGList(IR[button-1], Window, NULL, 1L);
  772.    }
  773.  
  774.    return(TRUE);
  775. }
  776.  
  777. /*------------------- I R B u t t o n L a b e l . c -------------------*/
  778. /* This routine relabels the selected button.                          */
  779. /*---------------------------------------------------------------------*/
  780.  
  781. int IRButtonLabel(buffer, button)
  782.   UBYTE *buffer;
  783.   int button;
  784. {
  785.    USHORT position;
  786.  
  787.    if((button >= 1) && (button <= 20))
  788.    {
  789.       /*--- Remove the selected gadget from list. */
  790.  
  791.       position = RemoveGList(Window, IR[button-1], 1L);
  792.  
  793.       /*--- Change the label. */
  794.  
  795.       strncpy(IR[button-1]->GadgetText->IText , (char *)buffer, 7);
  796.  
  797.       /*--- Put the gadget back into the same place in the list. */
  798.  
  799.       AddGList(Window, IR[button-1], (LONG)position, 1L, NULL);
  800.  
  801.       /*--- Refresh gadgets on display. */
  802.  
  803.       RefreshGList(IR[button-1], Window, NULL, 1L);
  804.    }
  805.  
  806.    return(TRUE);
  807. }
  808.  
  809.  
  810. /*------------------ G e t P a t t e r n . c ---------------------*/
  811. /* This routine gets a signal sample from the IR receiver and     */
  812. /* then tries up to four times to verify it.                      */
  813. /*----------------------------------------------------------------*/
  814.  
  815. GetPattern(button)
  816.   int button;  /* Currently selected control button. */
  817. {
  818.    int it, iz, is, try;
  819.    int diff[4], match, smallest;
  820.  
  821. #if DBG
  822.    printf("\n\n **** Storing pattern %d ****", button);
  823. #endif
  824.  
  825.    /*--- Get a sample. */
  826.  
  827.    GetSample();
  828.  
  829.    /*--- Plot data on screen. */
  830.  
  831.    helptxt("Data received.", "");
  832.    /*DPattern(button, 1);*/
  833.    DisplayPattern(button, 0);
  834.  
  835. #if DBG
  836.    printf("\n NSamples=%d", NSamples);
  837. #endif
  838.  
  839.    /*--- Transfer data to storage. */
  840.  
  841. #if DBG
  842.    printf("\n Xferring to pattern memory.");
  843. #endif
  844.    for(iz=0; iz<NSAMPLES; iz++)
  845.    {
  846.       pattern[button-1][iz] = zuffer[iz] & 0x01;
  847. #if DBG
  848. /*
  849.       if(pattern[button-1][iz] == 1)printf("|");
  850.       if(pattern[button-1][iz] == 0)printf("_");
  851. */
  852. #endif
  853.    }
  854.  
  855.    helptxt("Press the button again to verify the pattern.", "");
  856.  
  857.    match = 0;
  858.    for(try=0; try<4; try++)  /* Try four times to get a match. */
  859.    {
  860.       GetSample(); /* Read IR signal again. */
  861.  
  862.       helptxt("Data received.  Verifying match.", "");
  863. /*   DPattern(button, try+2);*/
  864.       DisplayPattern(button, 1);
  865.  
  866.       /*--- Find number of cells that don't match. */
  867.  
  868.       diff[try] = 0;
  869.       smallest = NSAMPLES+10;
  870.       for(iz=0; iz<NSAMPLES; iz++)
  871.       {
  872.          zuffer[iz] = zuffer[iz] & 0x01;
  873.          if(pattern[button-1][iz] != zuffer[iz])
  874.          {
  875.             diff[try]++;
  876.          }
  877. #if DBG
  878. /*
  879.          if(zuffer[iz] == 1)printf("|");
  880.          if(zuffer[iz] == 0)printf("_");
  881. */
  882. #endif
  883.       }
  884.       if(diff[try] > TOLERENCE)
  885.       {
  886. #if DBG
  887.          printf("\n Samples do not match:%d  Please try again.",diff[try]);
  888. #endif
  889.          helptxt("Samples do not match.  Please press button again.", "");
  890.          if(diff[try] < smallest)
  891.          {
  892.             /*--- Store the best pattern so far. */
  893.             smallest = diff[try];
  894.             for(iz=0; iz<NSAMPLES; iz++)
  895.             {
  896.                pattern[button-1][iz] = zuffer[iz];
  897.             }
  898.          }
  899.       } else
  900.       {
  901.          match = 1;
  902.          break;
  903.       }
  904.    }
  905.    if(match == 0)
  906.    {
  907.       helptxt("Could not verify pattern.  Will use the best match of four tries.", "");
  908. #if DBG
  909.       printf("\n Could not verify pattern.  Will use the best match.");
  910.       printf("\n Try the analyzer to verify compatability of your unit.");
  911. #endif
  912.    } else
  913.    {
  914.       helptxt("Successful pattern match!", "");
  915.       DisplayPattern(button, 0);
  916.    }
  917.  
  918.    return(TRUE);
  919. }
  920.  
  921.  
  922. /*-------------------- D i s p l a y P a t t e r n . c ------------------*/
  923. /* This routine draws the acquired pattern on the screen in the help     */
  924. /* message area.                                                         */
  925. /*-----------------------------------------------------------------------*/
  926.  
  927. int DisplayPattern(button, mode)
  928.   int button;  /* Currently selected control button. */
  929.   int mode;    /* 0 = draw pattern, 1 = draw difference. */
  930. {
  931.    int it;
  932.    SHORT row;
  933.  
  934.    /*--- Any old text or pattern that existed should have been erased with */
  935.    /*--- a help message. */
  936.  
  937.    /*--- Draw pattern. X range = 24 to 617 */
  938.  
  939.    SetAPen(WRPort, 1);
  940.    row = 0;
  941.    for(it=0; it<NSAMPLES; it++)
  942.    {
  943.       if(mode == 0)
  944.       {
  945.          if((zuffer[it] & 0x01) == 1)
  946.          {
  947.             Move(WRPort, 26 + (SHORT)(it%590), 175 + row);
  948.             Draw(WRPort, 26 + (SHORT)(it%590), 175 + row);
  949.          }
  950.       } else
  951.       {
  952.          if((zuffer[it] & 0x01) != pattern[button-1][it])
  953.          {
  954.             Move(WRPort, 26 + (SHORT)(it%590), 175 + row);
  955.             Draw(WRPort, 26 + (SHORT)(it%590), 175 + row);
  956.          }
  957.       }
  958.  
  959.       /*--- Space the data in rows. */
  960.  
  961.       if((it%590 == 0) &&(it != 0))row = row + 2;
  962.    }
  963. #if DBG
  964.    printf("\n Drew the pattern.");
  965. #endif
  966.  
  967.    return(TRUE);
  968. }
  969.  
  970.  
  971.  
  972. int DPattern(button, mode)
  973.   int button;  /* Currently selected control button. */
  974.   int mode;    /* 0 = draw pattern, 1 = draw difference. */
  975. {
  976.    int it;
  977.    SHORT row;
  978.  
  979.    /*--- Any old text or pattern that existed should have been erased with */
  980.    /*--- a help message. */
  981.  
  982.    /*--- Draw pattern. X range = 24 to 617 */
  983.  
  984.    SetAPen(WRPort, 1);
  985.    row = 0;
  986.    for(it=0; it<NSAMPLES; it++)
  987.    {
  988.       if((zuffer[it] & 0x01) == 1)
  989.       {
  990.          Move(WRPort, 26 + (SHORT)(it%590), 175 + row - mode*20);
  991.          Draw(WRPort, 26 + (SHORT)(it%590), 175 + row - mode*20);
  992.       }
  993.  
  994.       /*--- Space the data in rows. */
  995.  
  996.       if((it%590 == 0) &&(it != 0))row = row + 2;
  997.    }
  998.  
  999.    return(TRUE);
  1000. }
  1001. #endif /*DEBUG*/
  1002.  
  1003. /*-------------------- f i n d _ m a t c h . c ----------------------*/
  1004. /*  Find pattern which is the closest match to the one just read in. */
  1005. /*-------------------------------------------------------------------*/
  1006.  
  1007. int find_match()
  1008. {
  1009.    int match, it, smallest;
  1010.    int diff, pat;
  1011.  
  1012. #if DBG
  1013.    printf("\n Matching patterns...");
  1014. #endif
  1015.  
  1016.    /*--- Mask of bit of interest in zuffer. */
  1017.  
  1018.    for(it=0; it<NSAMPLES; it++)zuffer[it] = zuffer[it] & 0x01;
  1019.  
  1020.    /*--- Compare data to stored patterns. */
  1021.  
  1022.    match = -1;
  1023.    smallest = NSAMPLES+10;
  1024.    for(pat=0; pat<20; pat++)  /* Step through all stored patterns. */
  1025.    {
  1026.       if(active[pat+1])  /* Only examine active patterns to save time. */
  1027.       {
  1028.          diff = 0;
  1029.          for(it=0; it<NSAMPLES; it++)
  1030.          {
  1031.             if(pattern[pat][it] != zuffer[it])diff++;
  1032.          }
  1033.       }
  1034.       if(diff < smallest)
  1035.       {
  1036.          smallest = diff;
  1037.          match = pat;
  1038.       }
  1039. #if DBG
  1040.       printf("\n diff(%d)=%d", pat, diff);
  1041. #endif
  1042.    }
  1043.  
  1044. #if DBG
  1045.    printf("\n Best match is pattern %d", match);
  1046.    if(match == -1)
  1047.    {
  1048.       printf("\n No match for pattern!");
  1049.    }
  1050. #endif
  1051.  
  1052.    return(match);
  1053. }
  1054.